package cn.foodProject.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import cn.foodProject.R;
import cn.foodProject.adapter.FoodListAdapter;
import cn.foodProject.bean.MyOkhttp;
import cn.foodProject.bean.MyGson;
import cn.foodProject.bean.MyUrl;
import cn.foodProject.gsonBean.FoodGsonBean;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class FoodActivity extends AppCompatActivity {
    private static final String TAG = "FoodActivity";
    private Toolbar toolbarFood;
    private ListView list_food;
    private final Context context = FoodActivity.this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food);
        initView();
        new Thread(){
            @Override
            public void run() {
                super.run();
                updateList();
            }
        }.start();
    }

    private void initView() {
        toolbarFood = (Toolbar) findViewById(R.id.toolbar_food);
        list_food = (ListView) findViewById(R.id.list_food);

        //toolbar相关
        setSupportActionBar(toolbarFood);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbarFood.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //同步请求获取食物列表
    public void updateList() {
        try {
            Response response = MyOkhttp.getResponse(MyUrl.getUrl("/food/list"));
            //当请求成功时
            if (response.isSuccessful()) {
                //解析json
                String string = response.body().string();
                final FoodGsonBean foodGsonBean = (FoodGsonBean) MyGson.fromJson(string, FoodGsonBean.class);

                //当查询食物列表成功时,构建adapter并给listview使用
                if (foodGsonBean.isSuccess()) {
                    final FoodListAdapter listFoodAdapter = new FoodListAdapter(context, foodGsonBean.getData());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            list_food.setAdapter(listFoodAdapter);

                            //只有当获取列表成功后,listview的点击事件才生效
                            //点击后传递点击的食品的id并进入详情页
                            list_food.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    Intent intent = new Intent(context,FoodDetailsActivity.class);
                                    intent.putExtra("foodId", foodGsonBean.getData().get(position).getId());
                                    startActivity(intent);
                                }
                            });
                        }
                    });
                }else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(context, foodGsonBean.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            } else {
                Log.d(TAG, "onFailure: 请求失败");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, "失败!请检查输入或网络!", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
